'use strict';

const moment = require('moment');
const crypto = require('crypto');

module.exports = app => {
  const { Model, INTEGER, UUID, UUIDV4, STRING, ENUM, DATE, NOW } = app.Sequelize;
  class User extends Model {}
  User.init({
    id: { type: INTEGER, primaryKey: true, unique: true, autoIncrement: true, comment: '主键id' },
    uuid: { type: UUID, defaultValue: UUIDV4 },
    nickname: { type: STRING, allowNull: false },
    sex: { type: ENUM, values: [ 'male', 'female', 'unknown' ] },
    tel: { type: STRING(11), allowNull: false, unique: true, validate: { is: /^1(3|4|5|6|7|8|9)\d{9}$/, notNull: { msg: '请输入你的手机号' } } },
    avatar: { type: STRING },
    password: { type: STRING, allowNull: false, validate: { notNull: { msg: '请输入你的密码' } }, set(value) { this.setDataValue('password', crypto.createHash('md5').update(value).digest('hex')); } },
    username: { type: STRING(50) },
    create_time: { type: DATE, defaultValue: NOW, allowNull: false, get() { return moment(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss'); } },
    update_time: { type: DATE, defaultValue: 0, get() { return moment(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss'); } },
  },
  {
    sequelize: app.model,
    timestamps: false, // 关闭时间戳
    modelName: 'User',
  });
  return User;
};
